<template>
  <div class="login">
    <div class="login-container">
      <div class="image-system-title">
        <!-- <img src="../assets/images/icon.png" alt="icon"> -->
        <img src="../assets/images/system_title.png" alt="title" />
      </div>
      <div class="image-left">
        <!-- <img src="../assets/images/login-left-backgroud-image.png" alt=""> -->
      </div>
      <div class="form-right">

        <div class="login_input_box">
          <!-- 登录 -->
          <el-form v-if="login" ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
            <h3 class="title">用户登录</h3>
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
                <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
                @keyup.enter="handleLogin">
                <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>

            <el-form-item prop="code" v-if="captchaEnabled" style="width: 390px; justify-content: space-between">
              <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 62%"
                @keyup.enter="handleLogin">
                <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <!-- <el-checkbox
              v-model="loginForm.rememberMe"
              style="margin: 0px 0px 25px 0px"
              >记住密码</el-checkbox
            > -->
            <el-form-item>
              <el-button :loading="loading" size="large" color="#265DA4" style="
                  width: 394px;
                  background: linear-gradient(
                    -180deg,
                    #45bcff 0%,
                    #0090e1 100%
                  );
                " @click.prevent="handleLogin('login')">
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right" v-if="register">
                <router-link class="link-type" :to="'/register'">立即注册</router-link>
              </div>
            </el-form-item>
            <div class="footerBtn">
              <span @click.prevent="handleGoRegister">
                注册账户
              </span>
              <span @click.prevent="handleGoForget">
                忘记密码
              </span>
            </div>
          </el-form>
          <!-- 注册 -->
          <el-form v-if="register" ref="registerRef" :model="reginForm" :rules="registerRules" class="login-form">
            <h3 class="title">注册账户</h3>
            <el-form-item prop="username">
              <el-input v-model="reginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
                <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="reginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
                @keyup.enter="handleRegister">
                <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="phone">
              <el-input v-model="reginForm.phone" type="text" size="large" auto-complete="off" placeholder="请填写手机号/邮箱">
                <template #prefix><svg-icon icon-class="phone" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled" style="width: 390px; justify-content: space-between">
              <el-input v-model="reginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 62%"
                @keyup.enter="handleRegister">
                <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>

            <el-form-item>
              <el-button :loading="loadingReg" size="large" color="#265DA4" style="
                  width: 394px;
                  background: linear-gradient(
                    -180deg,
                    #45bcff 0%,
                    #0090e1 100%
                  );
                " @click.prevent="handleRegister">
                <span v-if="!loadingReg">注 册</span>
                <span v-else>注 册 中...</span>
              </el-button>

            </el-form-item>

          </el-form>
          <!-- 忘记密码 -->
          <el-form v-if="forget" ref="forgetRef" :model="forgetForm" :rules="forgetRules" class="login-form">
            <h3 class="title">忘记密码</h3>
            <el-form-item prop="phone">
              <el-input v-model="forgetForm.phone" type="text" size="large" auto-complete="off" placeholder="请填写手机号/邮箱">
                <template #prefix><svg-icon icon-class="phone" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="forgetForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
                @keyup.enter="handleForget">
                <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input v-model="forgetForm.confirmPassword" type="password" size="large" auto-complete="off"
                placeholder="确认密码" @keyup.enter="handleForget">
                <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled" style="width: 390px; justify-content: space-between">
              <el-input v-model="forgetForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 62%"
                @keyup.enter="handleForget">
                <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
              </el-input>
              <div class="forget-code">
                <span @click="getPhoneCode">{{ codeData.codeFlag ? codeData.time + 's' : codeData.codeValue }}</span>
                <!-- <img :src="codeUrl" @click="getCode" class="login-code-img" /> -->
              </div>
            </el-form-item>

            <el-form-item>
              <el-button :loading="loadingForger" size="large" color="#265DA4" style="
                  width: 394px;
                  background: linear-gradient(
                    -180deg,
                    #45bcff 0%,
                    #0090e1 100%
                  );
                " @click.prevent="handleForget">
                <span v-if="!loadingForger">确 定</span>
                <span v-else>提 交 中...</span>
              </el-button>

            </el-form-item>

          </el-form>
        </div>

      </div>
      <div class="image-right-title">
        <img src="../assets/images/footer_title.png" alt="title" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg, goregister } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";

const userStore = useUserStore();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: "",
});
const reginForm = ref({
  username: "",
  password: "",
  phone: '',
  code: "",
  uuid:""
})
const forgetForm = ref({
  phone: '',
  password: "",
  confirmPassword: "",
  code: "",
})
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};
const registerRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  // phone: [{ required: true, trigger: "blur", message: "请输入手机号/邮箱" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],

};
const forgetRules = {
  phone: [{ required: true, trigger: "blur", message: "请输入手机号/邮箱" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  confirmPassword: [{ required: true, trigger: "blur", message: "请输入确认密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],

};

const codeUrl = ref("");
const codeData = reactive({
  codeValue: "获取验证码",
  timer: null,
  time: 60,
  codeFlag: false
})
// 登录loading
const loading = ref(false);
// 注册loading
const loadingReg = ref(false);
// 忘记密码loading
const loadingForger = ref(false)
// 验证码开关
const captchaEnabled = ref(true);
// 登录开关
const login = ref(true);

// 注册开关
const register = ref(false);
// 忘记密码
const forget = ref(false)

const redirect = ref(undefined);

// 去注册
function handleGoRegister() {
  login.value = false
  register.value = true
}
// 去忘记密码页
function handleGoForget() {
  login.value = false
  forget.value = true
}
// 登录
function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {

          router.push({ path: redirect.value || "/index" });
        })
        .catch(() => {
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}

// 注册提交
function handleRegister() {
  proxy.$refs.registerRef.validate((valid) => {
    if (valid) {
      loadingReg.value = true;
      goregister(reginForm.value).then(res => {
        let data = res.data

      }).catch(error => {
        loadingReg.value = false;
      })
    }
  })
}
// 忘记密码提交
function handleForget() {
  proxy.$refs.forgetRef.validate((valid) => {
    if (valid) {
      loadingForger.value = true;

    }
  })

}
// 获取验证码
function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value =
      res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.data.img;
      loginForm.value.uuid = res.data.uuid;
      reginForm.value.uuid = res.data.uuid;
    }
  });
}

// 获取手机验证码
function getPhoneCode() {
  if (codeData.codeFlag) return
  codeData.codeFlag = true
  codeData.timer = null
  codeData.timer = setInterval(() => {
    if (codeData.time == 0) {
      codeData.codeFlag = false
      codeData.time = 60
    }
    codeData.time--
  }, 1000);
}
function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}

getCode();
getCookie();
</script>

<style lang='scss' scoped>
.login {
  height: 100%;
  background-image: url("../assets/images/new_login_backgroud_image1.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;

  .login-container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    // height: 100vh;
    // // width: 85%;
    // max-width: 1920px;
    // background-size: cover;
    // margin: 0 auto;
  }
}

.image-system-title {
  display: flex;
  align-items: center;
  position: absolute;
  left: 8%;
  top: 7%;

  img {
    margin-right: 10px;

    &:first-child {
      height: 103px;
    }

    &:last-child {
      height: 130px;
    }
  }
}

.image-footer-text {
  position: absolute;
  right: 14%;
  bottom: 3%;
}

.image-left {
  // margin-top: 200px;
}

.title {}

.form-right {
  // width: 500px;
  // position: absolute;
  // right: 5%;
  // top: 10%;

}

.image-right-title {
  width: 400px;
  height: 110px;
  position: absolute;
  bottom: 30px;
  right: 86px;

  img {
    width: 100%;
    height: 100%;
  }
}

.login_input_box {
  width: 529px;
  height: 582px;
  background-image: url('../assets/images/login_form_bg.png');
  // background: #B9C1C7;
  border-radius: 16px 16px 16px 16px;
  display: flex;
  justify-content: center;
  align-items: center;
  // background-image: url("../assets/images/login_input_bg.png");
  background-size: cover;
  // margin-top: 30px;
}

.inputDeep {
  :deep(.el-input__wrapper) {
    background-color: #fff;
    border: 1px solid #D9D9D9;
    box-shadow: none;
  }

  :deep(.el-input__inner) {
    color: #fff;
  }
}

.login-form {
  height: 426px;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: space-around;

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #fff;
    font-size: 30px;
    font-weight: bold;
  }

  .el-input {
    width: 390px;
    height: 40px;

    input {
      height: 40px;
    }

  }

  .footerBtn {
    width: 100%;
    display: flex;
    justify-content: space-between;
    color: #fff;
    margin-top: -20px;
    font-size: 16px;
    cursor: pointer;
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;

  }
}

.el-checkbox {
  color: #fff;
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  // width: 33%;
  flex: 1;
  height: 40px;
  float: right;
  background: #fff;
  margin-left: 12px;

  img {
    cursor: pointer;
    vertical-align: middle;
    width: 100%;
    height: 40px;
  }
}

.forget-code {
  flex: 1;
  height: 40px;
  float: right;
  margin-left: 12px;
  cursor: pointer;

  span {
    color: #fff;
    text-decoration: underline;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
